package com.lujun;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.*;

public class LuJunCombine {
    public static void combine(String path,String outPath) throws Exception {
		File file1 = new File(path);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
		File outFilePath =   new File(outPath);
		if(!outFilePath.exists()){ outFilePath.mkdir(); }
        WritableWorkbook book = Workbook.createWorkbook(new File( outPath + "\\".concat(sdf2.format(new Date()))  + ".xls"));
        WritableSheet sheet = book.createSheet("数据", 0);
        File[] listFiles = file1.listFiles();
        Map<Integer, List<String>> map = new HashMap<>();
        for (File file : listFiles) {
            Reader in = new FileReader(path + "\\" + file.getName());
            List<String> list = new ArrayList<String>();
            Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
            for (CSVRecord record : records) {
                if (record.get(1).equals("Flicker")) {
                    continue;
                }
                list.add(record.get(1));
                String names = file.getName().substring(0, file.getName().lastIndexOf("."));
                map.put(Integer.parseInt(names), list);
            }
        }
        int j = 0;
        for(Integer key : map.keySet()){
            List value = map.get(key);
            value.add(0,key);
            for(int i = 0;i <value.size();i++){
            	Label cell =  new Label(j,i, value.get(i).toString());
                sheet.addCell(cell);
            }
            j++;
        }
        book.write();
        book.close();
    	}
   }
